//
//  common.swift
//  AutoModel
//
//  Created by 好米有易 on 2019/8/1.
//  Copyright © 2019 own. All rights reserved.
//

import Cocoa

let MIN_HEIGHT = 840
let MIN_WIDTH = 1440
let MIN_SIZE = CGSize(width: MIN_WIDTH, height: MIN_HEIGHT)


let spaceString = "    "

/// 生成Int属性字符串
func makeInt(property: String) -> String {
    return spaceString + "let \(property): Int\n"
}

/// 生成NSNull属性字符串
func makeNSNull(property: String) -> String {
    return spaceString + "var \(property): NSNull?\n"
}

/// 生成String属性字符串
func makeString(property: String) -> String {
    return spaceString + "let \(property): String\n"
}

/// 生成Object属性字符串
func makeObject(property: String) -> String {
    return spaceString + "var \(property): \(property.capitalized)?\n"
}

/// 生成Objects属性字符串
func makeObjects(property: String) -> String {
    return spaceString + "var \(property) = [\(property.capitalized)]()\n"
}


/// Int类型属性赋值
///
/// - Parameter property: 属性字符串
func initInt(property: String)  -> String {
    return spaceString + #"\#(property) = dict["\#(property)"] as? Int ?? 0\#n"#
}

/// String类型属性赋值
///
/// - Parameter property: 属性字符串
func initString(property: String)  -> String {
    return spaceString + #"\#(property) = dict["\#(property)"] as? String ?? ""\#n"#
}

/// NSNull类型属性赋值
///
/// - Parameter property: 属性字符串
func initNSNull(property: String)  -> String {
    return spaceString + #"\#(property) = dict["\#(property)"] as? NSNull ?? NSNull()\#n"#
}


/// 对象初始化
///
/// - Parameters:
///   - property: 对象字符串
///   - spacing: 间隔
func initObject(property: String, spacing: String)  -> String {
    let rowHeader = spacing + spaceString + #"if let \#(property)Dict = dict["\#(property)"] as? [String: Any] {\#n"#
    let rowBody = spacing + spaceString + spaceString + "\(property) = \(property.capitalized)(dict: \(property)Dict)\n"
    let rowFooter = spacing + spaceString + "}\n"
    return rowHeader + rowBody + rowFooter
}

/// 对象初始化数组
///
/// - Parameters:
///   - property: 对象字符串
///   - spacing: 间隔
func initObjects(property: String, spacing: String)  -> String {
    let rowHeader = spacing + spaceString + #"if let \#(property)s = dict["\#(property)"] as? [[String: Any]] {\#n"#
    let rowBody = forInitialize(property: property, spacing: spaceString  + spacing)
    let rowFooter = spacing + spaceString + "}\n"
    return rowHeader + rowBody + rowFooter
}


/// 对象循环遍历
///
/// - Parameters:
///   - property: 要循环对象字符串
///   - spacing: 间隔
/// - Returns: 返回循环对象字符串
func forInitialize(property: String, spacing: String) -> String {
    let rowHeader = spacing + spaceString + "for \(property)Dict in \(property)s {\n"
    let rowBody = spacing + spaceString + spaceString + "let elementObj = \(property.capitalized)(dict: \(property)Dict)\n" + spacing + spaceString + spaceString + "\(property).append(elementObj)\n"
    let rowFooter = spacing + spaceString + "}\n"
    return rowHeader + rowBody + rowFooter
}

/// 结构体初始化方法
///
/// - Parameter assignment: 初始化方法内容字符串
/// - Returns: 结构体初始化方法
func structInitialize(assignment: String) -> String {
    let rowHeader = spaceString + "init(dict: [String: Any]) {\n"
    let rowBody = assignment
    let rowFooter = spaceString + "}"
    return rowHeader + rowBody + rowFooter
}
